<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>props的基本使用</title>
</head>
<body>
<!--准备好一个容器-->
<div id="test">

</div>
<div id="test2">

</div>
<div id="test3">

</div>
</body>

<!--核心库引入顺序，必须先是核心库然后在引入dom库-->
<script src="../js/react.development.js" crossorigin></script>
<!--用于支持react操作dom-->
<script src="../js/react-dom.development.js"></script>
<!--引入babel，用于将jsx转为js-->
<script src="../js/babel.min.js"></script>
<!--引入prop-types，用于对组件标签属性进行限制-->
<script src="../js/prop-types.js"></script>

<script type="text/babel">
    //创建组件
    class Person extends React.Component {
        constructor(props) {
            //构造器是否接收props，是否传递给super，取决于，是否希望中通过this访问props
            super(props);
            console.log('constructor', this.props)
        }

        static propTypes = {
            name: PropTypes.string.isRequired,  //限制name必传，且为字符串
            sex: PropTypes.string,  //限制sex为字符串
            age: PropTypes.number,  //现实性别为数字
            speak: PropTypes.func   //限制speak为方法
        }

        static defaultProps = {
            sex: '男',//sex默认男
            age: 18//年龄默认是18
        }

        render() {
            let {name, age, sex} = this.props;
            //props是只读的
            //this.props.name = 'jack' //此行代码报错，因为props是只读的
            return (
                <ul>
                    <li>姓名：{name}</li>
                    <li>性别：{age + 1}</li>
                    <li>年龄：{sex}</li>
                </ul>
            )
        }
    }

    //2.渲染组件到页面
    ReactDOM.render(<Person name="tom" speak={speak}/>, document.getElementById('test'))
    ReactDOM.render(<Person name="julia" age={20} sex="女"/>, document.getElementById('test2'))
    const person = {name: 'tom', sex: '女', age: 23}
    //ReactDOM.render(<Person name={person.name} age={person.age} sex={person.age}/>, document.getElementById('test3'))
    ReactDOM.render(<Person {...person}/>, document.getElementById('test3'))

    function speak() {
        console.log('说话')
    }
</script>
</html>